package com.service.impl.user;

import com.core.dto.PageDTO;
import com.core.dto.RoleDTO;
import com.core.model.user.Role;
import com.core.shared.DateTimeUtils;
import com.dao.RoleDao;
import com.web.handle.RedisTemplateHandle;
import com.service.user.RoleService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: lxf
 * Date: 2016/6/27
 * Time: 17:14
 * To change this template use File | Settings | File Templates.
 */
@Service("roleService")
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;
    @Autowired
    private RedisTemplateHandle redisTemplateHandle;


    @Override
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    //readOnly: 防止dirty check  NOT_SUPPORTED:不以事务执行
    public Map<String, Object> roleList(String currentPage) {
        Map<String, Object> map = new HashedMap();
        String totalPage = redisTemplateHandle.getInfoString("totalPage", "role", Role.class, roleDao);
        currentPage = PageDTO.isPage(currentPage, totalPage);
        List<Role> roles = redisTemplateHandle.getInfoList("roleList", currentPage, Role.class, roleDao);
        List<RoleDTO> roleDTOs = RoleDTO.toDTOs(roles);
        map.put("roleDTOs", roleDTOs);
        map.put("currentPage", currentPage);
        map.put("totalPage", totalPage);
        return map;
    }

    @Override
    @Transactional
    public boolean saveOrUpdateRole(RoleDTO roleDTO) {
        boolean result = true;
        try {
            Role role = RoleDTO.toRole(roleDTO);
            Role find = roleDao.findRoleByGuid(roleDTO.getGuid());
            if (find == null) {
                roleDao.saveOrUpdate(role);
                redisTemplateHandle.updateEntity("roleList", role.guid(), role);
            } else {
                find.initRole(role);
                find.setUpdateTime(DateTimeUtils.getLocalDateTime());
                roleDao.saveOrUpdate(find);
                redisTemplateHandle.updateEntity("roleList", find.guid(), find);
            }
            redisTemplateHandle.delValue("totalPage", "role");
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        }
        return result;
    }

    @Override
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public RoleDTO findRoleByGuid(String roleGuid) {
        Role role = redisTemplateHandle.findEntityByGuid("roleList", roleGuid, Role.class, roleDao);
        RoleDTO roleDTO = RoleDTO.toDTO(role);
        return roleDTO;
    }

    @Override
    @Transactional
    public boolean delRoleByGuid(String roleGuid) {
        boolean result = true;
        try {
            Role role = roleDao.findIsExistByGuid(Role.class, roleGuid);
            if (role != null) {
                roleDao.delObject(role);
            }
            redisTemplateHandle.delEntity("roleList", roleGuid, role);
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        }
        return result;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<RoleDTO> findAllRole() {
        List<Role> roles = roleDao.findAll(Role.class);
        List<RoleDTO> roleDTOs = RoleDTO.toDTOs(roles);
        return roleDTOs;
    }
}
